// 获取所有class为stock-control的元素
const stockControls = document.querySelectorAll('.stock-control');

// 为每个库存组件绑定JS动作
stockControls.forEach((control) => {
    const stockInput = control.querySelector('.stock-input');
    const minusBtn = control.querySelector('.minus-btn');
    const plusBtn = control.querySelector('.plus-btn');

    minusBtn.addEventListener('click', () => {
        let stockCount = parseInt(stockInput.value);
        if (stockCount > 0) {
            stockCount--;
            stockInput.value = stockCount;
            updateStock(stockCount);
        }
    });

    plusBtn.addEventListener('click', () => {
        let stockCount = parseInt(stockInput.value);
        stockCount++;
        stockInput.value = stockCount;
        updateStock(stockCount);
    });

    function updateStock(stockCount) {
        const itemId = $(control).parents('tr').children('#thisItemId').text(); // 获取产品ID
        console.log(itemId);
        const data = {
            itemId: itemId,
            stockCount: stockCount
        };
        $.ajax({
            type: 'POST',
            url: "/Items/updateItemQty",
            data: JSON.stringify(data),
            contentType: 'application/json',
            success:function (){}
        });
    }
});

